﻿using CAION2.Classes;
using CAION2.Commands;

namespace CAION2.ViewModels
{
    class ExtendedFilterForRecipesDialogViewModel : ViewModelBase
    {
        #region Vars

        private int _filterRecipesMinLevel;
        private int _filterRecipesMaxLevel;
        private int _filterCharacterMinLevel;
        private int _filterCharacterMaxLevel;
        private bool _filterElyosRecipes;
        private bool _filterAsmodianRecipes;
        private SampleCollection<bool> _filterQuality;

        #endregion

        #region Properties

        public int FilterRecipesMinLevel
        {
            get { return _filterRecipesMinLevel; }
            set
            {
                if (_filterRecipesMinLevel == value) return;

                _filterRecipesMinLevel = value;
                OnPropertyChanged("FilterRecipesMinLevel");

                //if (_filterRecipesMinLevel > _filterRecipesMaxLevel)
                //{
                //    FilterRecipesMaxLevel = value;
                //}
                
                ClassConfiguration.SetValue("RecipesMinLevel", _filterRecipesMinLevel);
            }
        }

        public int FilterRecipesMaxLevel
        {
            get { return _filterRecipesMaxLevel; }
            set
            {
                if (_filterRecipesMaxLevel == value) return;

                _filterRecipesMaxLevel = value;
                OnPropertyChanged("FilterRecipesMaxLevel");

                //if (_filterRecipesMinLevel > _filterRecipesMaxLevel)
                //{
                //    FilterRecipesMinLevel = value;
                //}
                
                ClassConfiguration.SetValue("RecipesMaxLevel", _filterRecipesMaxLevel);
            }
        }

        public int FilterCharacterMinLevel
        {
            get { return _filterCharacterMinLevel; }
            set
            {
                if (_filterCharacterMinLevel == value) return;

                _filterCharacterMinLevel = value;
                OnPropertyChanged("FilterCharacterMinLevel");

                //if (_filterCharacterMinLevel > _filterCharacterMaxLevel)
                //{
                //    FilterCharacterMaxLevel = value;
                //}
              
                ClassConfiguration.SetValue("CharacterMinLevel", _filterCharacterMinLevel);
            }
        }

        public int FilterCharacterMaxLevel
        {
            get { return _filterCharacterMaxLevel; }
            set
            {
                if (_filterCharacterMaxLevel == value) return;

                _filterCharacterMaxLevel = value;
                OnPropertyChanged("FilterCharacterMaxLevel");

                //if (_filterCharacterMinLevel > _filterCharacterMaxLevel)
                //{
                //    FilterCharacterMinLevel = value;
                //}
            
                ClassConfiguration.SetValue("CharacterMaxLevel", _filterCharacterMaxLevel);
            }
        }

        public bool FilterElyosRecipes
        {
            get { return _filterElyosRecipes; }
            set
            {
                if (_filterElyosRecipes == value) return;

                _filterElyosRecipes = value;
                OnPropertyChanged("FilterElyosRecipes");
                
                ClassConfiguration.SetValue("RecipesRaceE", _filterElyosRecipes);
            }
        }

        public bool FilterAsmodianRecipes
        {
            get { return _filterAsmodianRecipes; }
            set
            {
                if (_filterAsmodianRecipes == value) return;

                _filterAsmodianRecipes = value;
                OnPropertyChanged("FilterAsmodianRecipes");
                
                ClassConfiguration.SetValue("RecipesRaceA", _filterAsmodianRecipes);
            }
        }

        public SampleCollection<bool> FilterQuality
        {
            get { return _filterQuality; }
            set
            {
                if (_filterQuality == value) return;

                _filterQuality = value;
                OnPropertyChanged("FilterQuality");
            }
        }

        public static int MaximumCharacterLevel
        {
            get { return ClassConfiguration.CharacterMaximumLevel; }
        }

        public static int MaximumRecipesLevel
        {
            get { return ClassConfiguration.RecipesMaximumLevel; }
        }
        
        #endregion

        #region Commands

        public RelayCommand<string> ResetFilterCommand { get; private set; }
        public RelayCommand FilterQualityCommand { get; private set; }

        #endregion


        public ExtendedFilterForRecipesDialogViewModel()
        {
            WireCommands();

            FilterRecipesMinLevel = ClassConfiguration.GetInt32("RecipesMinLevel");
            FilterRecipesMaxLevel = ClassConfiguration.GetInt32("RecipesMaxLevel");
            FilterCharacterMinLevel = ClassConfiguration.GetInt32("CharacterMinLevel");
            FilterCharacterMaxLevel = ClassConfiguration.GetInt32("CharacterMaxLevel");

            FilterElyosRecipes = ClassConfiguration.GetBool("RecipesRaceE");
            FilterAsmodianRecipes = ClassConfiguration.GetBool("RecipesRaceA");

            FilterQuality = new SampleCollection<bool> { ClassConfiguration.GetBool("RecipesQualityCommon"), 
                                                         ClassConfiguration.GetBool("RecipesQualityRare"), 
                                                         ClassConfiguration.GetBool("RecipesQualityLegend"), 
                                                         ClassConfiguration.GetBool("RecipesQualityUnique"), 
                                                         ClassConfiguration.GetBool("RecipesQualityEpic"), 
                                                         ClassConfiguration.GetBool("RecipesQualityMythic") };
        }

        private void WireCommands()
        {
            ResetFilterCommand = new RelayCommand<string>(ResetFilter) { IsEnabled = true };
            FilterQualityCommand = new RelayCommand(SetFilterQuality) { IsEnabled = true };
        }
        
        private void ResetFilter(string flag)
        {
            switch (flag)
            {
                case "RecipesLevel":
                    FilterRecipesMinLevel = 1;
                    FilterRecipesMaxLevel = MaximumRecipesLevel;
                    break;
                case "CharacterLevel":
                    FilterCharacterMinLevel = 1;
                    FilterCharacterMaxLevel = MaximumCharacterLevel;
                    break;
            }
        }

        private void SetFilterQuality()
        {
            ClassConfiguration.SetValue("RecipesQualityCommon", FilterQuality[0]);
            ClassConfiguration.SetValue("RecipesQualityRare", FilterQuality[1]);
            ClassConfiguration.SetValue("RecipesQualityLegend", FilterQuality[2]);
            ClassConfiguration.SetValue("RecipesQualityUnique", FilterQuality[3]);
            ClassConfiguration.SetValue("RecipesQualityEpic", FilterQuality[4]);
            ClassConfiguration.SetValue("RecipesQualityMythic", FilterQuality[5]);
        }

    }
}
